﻿Namespace nHibernate.Models
    Public Class CustomerModel
        Public Sub New()
        End Sub

        Public Overridable Property CustomerID As String
        Public Overridable Property DisplayName As String
        Public Overridable Property IsCompany As Boolean
        Public Overridable Property Preisgruppe As Preisgruppe

        Public Overridable Property PaymentMethode As nHibernate.Models.PaymentMethodeModel
        Public Overridable Property Contact As nHibernate.Models.ContactModel
        Public Overridable Property Address As nHibernate.Models.AddressModel
        Public Overridable Property Addresses As IList(Of nHibernate.Models.EAddressModel)

        Private Function ToEAddressesCollection() As Collections.ObjectModel.ObservableCollection(Of EAddress)
            Dim o As New Collections.ObjectModel.ObservableCollection(Of EAddress)
            For Each item In Addresses
                o.Add(item.ToEAddress)
            Next
            Return o
        End Function

        Public Overridable Function ToCustomer() As Customer
            Return New Customer With {.CustomerID = CustomerID, .IsCompany = IsCompany, .PaymentMethode = PaymentMethode.ToPaymentMethode, .Preisgruppe = Preisgruppe, .Address = Address.ToAddress, .Contact = Contact.ToContact, .Addresses = ToEAddressesCollection()}
        End Function

        Public Shared Function FromCustomer(c As Customer) As CustomerModel
            Dim t As New CustomerModel With {.CustomerID = c.CustomerID, .DisplayName = c.DisplayName, .IsCompany = c.IsCompany, .Preisgruppe = c.Preisgruppe, .PaymentMethode = PaymentMethodeModel.FromPaymentMethode(c.PaymentMethode), _
                                             .Contact = ContactModel.FromContact(c.Contact, c.CustomerID), .Address = AddressModel.FromAddress(c.Address, c.CustomerID)}
            Dim ea As New List(Of EAddressModel)
            For Each item In c.Addresses
                ea.Add(EAddressModel.FromAddress(item, c.CustomerID))
            Next
            t.Addresses = ea
            Return t
        End Function

    End Class
End Namespace
